import pymysql
host = 'localhost'
username = 'root'
password = 'ft_10086'
db = 'auto_demo'
new_age = 99


def conn_mysql():
    global conn;
    conn = pymysql.Connect(
        host=host,
        port=3306,
        user=username,
        passwd=password,
        db=db,
        charset='utf8'
    )
    cursor = conn.cursor()
    return cursor


def do_exchange(cursor):
    # cursor = conn_mysql()
    execute = cursor.execute('show tables;')
    # print(execute)
    tables = cursor.fetchall()
    # print(tables)
    for obj in tables:
        table = obj[0]
        # print(table)
        rows = cursor.execute("select * from %s limit 0, 1;" % table)
        # print(rows)
        if rows:
            # 返回游标活动状态
            columns = [col[0] for col in cursor.description]
            # print(columns)
            if "age" in columns:
                res = cursor.execute("update {0} set age = {1};".format(table, new_age))
                conn.commit()
                print(table + '\t' + str(res))
            elif "tage" in columns:
                res = cursor.execute("update {0} set tage = {1};".format(table, new_age))
                conn.commit()
                print(table + '\t' + str(res))


def object_close(cursor):
    cursor.close
    conn.close


def main():
    cursor = conn_mysql()
    do_exchange(cursor)
    object_close(cursor)


if __name__ == '__main__':
    main()
